#!/bin/bash 

export WAFER_DIR=/root/wafer_project/wafer-compiler
cd $WAFER_DIR/3rdparty/sim_lib
source ./env
cd -
# if [ "$#" -ne 2 ]; then
#     echo "Usage: $0  <created_folder_path> <gpu_mode>"
#     exit 1
# fi

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
tool_path="$SCRIPT_DIR"
work_path="$SCRIPT_DIR/../result"
config_path="$SCRIPT_DIR/../configs"
wafer_copy_path="$SCRIPT_DIR/../wafer_copy"
gpu_mode=0
`date +"[%Y-%m-%d %H:%M:%S] 开始构建仿真配置" >> $work_path/project.log` 
echo $work_path
echo $gpu_mode
#Generate gpu config
$tool_path/gpu_config_gen.py -c ../configs/gpgpusim.config -j $wafer_copy_path/design.json -o $work_path/gpgpusim.config -m $gpu_mode
cp $config_path/config_fermi_islip.icnt $work_path

#caculate np num
$tool_path/cal_np.py -j $wafer_copy_path/design.json -o $work_path/np_tmp
np=`head -1 $work_path/np_tmp`
echo $np

# rm $work_path/run.log $work_path/runtime.log

#replace exe
cp $config_path/selfcfg.cfg $work_path
replacement_content=$work_path/wafer_run_exe
echo $replacement_content
escaped_content=$(echo "$replacement_content" | sed 's/[\/&]/\\&/g')
sed -i '4s/.*/executable: '"$escaped_content"'/' $work_path/selfcfg.cfg

cp "$tool_path/utils_design.py" "$work_path/"
cp "$tool_path/design.py" "$work_path/"


cd $work_path
`date +"[%Y-%m-%d %H:%M:%S] 开始运行仿真任务" >> project.log` 
# echo $PWD
mpirun -np $np --oversubscribe sst $work_path/design.py --output-dot=configur.dot --model-options="-j $wafer_copy_path/design.json -c $work_path/selfcfg.cfg -s $work_path/statOutput.csv -g $work_path/gpu_sum.json"  | tee $work_path/run.log

# # echo "ok"
# grep -a "simulated time" $work_path/run.log | awk -F " " '{print $6}' > $work_path/runtime.log
# `date +"[%Y-%m-%d %H:%M:%S] 仿真完成" >> project.log` 

# `date +"[%Y-%m-%d %H:%M:%S] 开始绘图" >> project.log`

# cd $work_path
# mkdir results
# cp $tool_path/plot.py .
# cat statOutput_*.csv |grep -v ComponentName > statOutput.csv
# python plot.py $work_path/statOutput.csv 
# `date +"[%Y-%m-%d %H:%M:%S] 完成绘图" >> project.log`

